package top.bbman.boot.redis.bean;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import top.bbman.boot.logger.LogConstant;
import top.bbman.boot.redis.constant.PeaRedisBeanName;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author 祥霸
 * @version 1.0.0
 */
@Component(PeaRedisBeanName.DIY_KEY_REDIS)
public class PeaKeyRedis {
    @PostConstruct
    private void init() {
        LogConstant.bbManRedis.info("init key redis");
    }

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * remove redis key
     *
     * @param key redis key
     * @return <tt>true</tt>
     */
    public Boolean remove(String key) {
        return stringRedisTemplate.delete(key);
    }

    /**
     * set key timeout
     *
     * @param key redis key
     * @param timeout timeout
     * @param unit unit
     * @return <tt>true</tt>
     */
    public Boolean expire(final String key, long timeout, final TimeUnit unit) {
        return stringRedisTemplate.expire(key, timeout, unit);
    }

    /**
     * 取消过期时间
     *
     * @param key redis key
     * @return <tt>true</tt>
     */
    public Boolean persist(final String key) {
        return stringRedisTemplate.persist(key);
    }

    /**
     * 获取剩余存活时长
     * @param key redis key
     * @param unit unit
     * @return -1-无过期时间；-2-key不存在
     */
    public Long getExpire(final String key, final TimeUnit unit) {
        return stringRedisTemplate.getExpire(key, unit);
    }

    /**
     * redis 包含 key
     *
     * @param redisKey key
     * @return TRUE-存在
     */
    public Boolean contain(String redisKey) {
        return stringRedisTemplate.hasKey(redisKey);
    }

}
